import {Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChange} from "@angular/core";

@Component({
  selector: "portlet-action",
  template: `
    <div class="actions" *ngIf="actionBtns.length">
      <ng-container *ngFor="let item of actionBtns">
        <a *ngIf="item.show&&item.disabled" href="javascript:void(0)" class="btn btn-default blue"
           [title]="item.title"
           (click)="acitonClick(item)" disabled>
          <i class="fa" [ngClass]="item.icon"></i> {{item.title}}
        </a>
        <a *ngIf="item.show&&!item.disabled" href="javascript:void(0)" class="btn btn-default blue"
           [title]="item.title"
           (click)="acitonClick(item)">
          <i class="fa" [ngClass]="item.icon"></i> {{item.title}}
        </a>
      </ng-container>
    </div>
  `,
  styles: [
    ".actions a{margin-right: 10px}"
  ]
})
export class PortletAcitonComponent implements OnInit, OnChanges, OnDestroy {
  actionBtns: any[] = [];
  icons: any = {
    add: "fa-plus",
    removeN: "fa-bank",
    refresh: "fa-refresh",
    upload: "fa-upload",
    exceldr: "fa-sign-in",
    szptdr: "fa-sign-in",
    fp: "fa-exchange",
    fp2: "fa-exchange",
    export: "fa-share",
    exceldc: "fa-share",
    dbfdc: "fa-share",
    pdfdc: "fa-share",
    szptdc: "fa-share",
    dc: "fa-share",
    exchange: "fa-arrows",
    copy: 'fa-crop',
    editeN: 'fa-pencil',
    dr: 'fa-sign-in',
    sdztdr: "fa-sign-in",
    xz: 'fa-sign-in',
    lock: "fa-lock",
    setrcN: 'fa-user-plus',
    setqkN: "fa-user-times",
    xtsc: "fa-hand-paper-o",
    bindgx: "fa-connectdevelop",
    zxt: "fa-line-chart"
  }


  acitonClick(item: any): void {
    this.outhander.emit(item);
  }

  /**
   pfrom: "sjb",
   title: "刷新",
   type: 'refresh',
   show: true,
   disabled:true---禁止点击
   */
  @Input() o: any;
  @Output() outhander: EventEmitter<any> = new EventEmitter<any>();

  constructor() {

  }

  ngOnInit() {
    console.log('进入了按钮渲染组件');
    
    const actions = this.o;
    this.actionBtns = actions.map((item) => {
      const icontype = item.type;
      item.icon = this.icons[icontype];
      return item;
    });
  }

  ngOnChanges(changes: { [hs: string]: SimpleChange }) {
    const firstchange = changes.o.firstChange;
    if (!firstchange) {
      const actions = this.o;
      this.actionBtns = actions.map((item) => {
        const icontype = item.type;
        item.icon = this.icons[icontype];
        return item;
      });
    }
  }

  ngOnDestroy() {

  }

}
